/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.thread;

import application.ListOption;
import exception.UknownProtocolCodeException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import protocol.ProtocolCode;
import stream.ByteCoding;
import window.ChatWindow;

/**
 *
 * @author Simão
 */
public class PeerThread extends ProtocolThread {

    private String username;
    private boolean iAmPeerServer;
    private InetAddress address;
    private int port;
    private ChatWindow chatWindow;

    public PeerThread(Socket socket, ChatWindow chatWindow) {
        super(socket);
        iAmPeerServer = false;
    }

    public PeerThread(ChatWindow chatWindow, InetAddress address, int port, String username) {
        super();
        this.address = address;
        this.port = port;
        this.username = username;
        iAmPeerServer = true;
    }

    @Override
    public void run() {
        ReaderThread serverReader;
        WriterThread serverWriter;
        InputStream inputStream;
        DataInputStream dataInputStream;
        OutputStream outputStream;
        DataOutputStream dataOutputStream;
        ProtocolCode protocolCode;
        byte[] stream;
        if (iAmPeerServer) {
            try {
                socket = new Socket(address, port);
            } catch (IOException ex) {
                System.out.println("Couldn't access the other Peer. Aborting connection.");
                return;
            }
        }
        System.out.println("Connection estabilished.");
        try {
            outputStream = socket.getOutputStream();
            dataOutputStream = new DataOutputStream(outputStream);
            System.out.println("Socket DataOutputStream initialized.");
            inputStream = socket.getInputStream();
            dataInputStream = new DataInputStream(inputStream);
            System.out.println("Socket DataInputStream initialized.");
        } catch (IOException ex) {
            System.out.println("IOException in socket. Aborting connection.");
            try {
                socket.close();
            } catch (IOException ex1) {
                System.out.println("IOException while closing socket.");
            }
            return;
        }
        serverReader = new ReaderThread(dataInputStream, readerSemaphore, this);
        serverWriter = new WriterThread(dataOutputStream, writerSemaphore, this);
        serverReader.start();
        serverWriter.start();
        /* Open chat window here */
        while (true) {
            try {
                System.out.println("Waiting for messages from ReaderThread.");
                readerSemaphore.acquire();
            } catch (InterruptedException ex) {
                System.out.println("InterruptedException in synchronization point. Aborting connection.");
                try {
                    socket.close();
                } catch (IOException ex1) {
                    System.out.println("IOException while closing socket.");
                }
                return;
            }
            stream = useReaderList(ListOption.REMOVE,null);
            System.out.println("Message obtained from ReaderList.");
            try {
                protocolCode = ByteCoding.setAsProtocolCode(stream);
                System.out.println("Message type identified and valid.");
            } catch (UknownProtocolCodeException ex) {
                System.out.println("Message type is invalid. Ignoring message.");
                continue;
            }
            switch (protocolCode) {
                /* process other protocol message types */
                default:
                    System.out.println("Message with unexcpected type received. Ignoring message.");
            }
        }
    }
}
